package com.platform.common.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * HttpURLConnection 工具类，获取请求信息
 * 
 * @author Administrator
 * 
 */
public class HttpURLConnUtil {
	/**
	 * 根据URL请求接口并将数据返回
	 * 
	 * @param interFaceUrl
	 *            接口URL
	 * @return
	 */
	public static String getData(String interFaceUrl) {
		StringBuilder sb = new StringBuilder();
		try {
			URL url = new URL(interFaceUrl); // 把字符串转换为URL请求地址
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 打开连接
			connection.setConnectTimeout(30000); // 设置连接主机超时（单位：毫秒）
			connection.setReadTimeout(30000); // 设置从主机读取数据超时（单位：毫秒）
			connection.connect();// 连接会话
			// 获取输入流
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
			String line;
			while ((line = br.readLine()) != null) {// 循环读取流
				sb.append(line);
			}
			br.close();// 关闭流
			connection.disconnect();// 断开连接
			System.out.println(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("失败!");
		}
		return sb.toString();
	}
}
